//
//  GlobalVC.m
//  ApeExample
//
//  Created by Eton on 2017/7/27.
//  Copyright © 2017年 王-庭协. All rights reserved.
//

#import "GlobalVC.h"
#import "LaunchViewController.h"
#import "LoginViewController.h"
#import "AppTabBarController.h"

@interface GlobalVC ()

@end

@implementation GlobalVC

+ (GlobalVC *)shared {
    
    static GlobalVC *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[GlobalVC alloc] init];
    });
    
    return instance;
}

#pragma mark - init
- (LaunchViewController *)launchVC{
    if (!_launchVC) {
        _launchVC = [[LaunchViewController alloc] init];
    }
    
    return _launchVC;
}

- (AppTabBarController *)tabBar{
    if (!_tabBar) {
        _tabBar = [[AppTabBarController alloc] init];
    }
    return _tabBar;
}

- (ApeNavigationController *)homeNC{
    if (!_homeVC) {
        _homeVC = [[ApeViewController alloc] init];
    }
    ApeNavigationController *homeNav = [[ApeNavigationController alloc] initWithRootViewController:_homeVC];
    return homeNav;
}

- (ApeNavigationController *)loginNC{
    LoginViewController * loginVC = kVCFromSb(@"Auth", NSStringFromClass([LoginViewController class]));
    ApeNavigationController *mainLoginNav = [[ApeNavigationController alloc] initWithRootViewController:loginVC];
    
//    [mainLoginNav.navigationBar setBackgroundImage:[UIImage tx_imageWithColor:gNavBackgroundColor] forBarMetrics:UIBarMetricsDefault];
    [mainLoginNav.navigationBar setShadowImage:[UIImage tx_imageWithColor:[UIColor clearColor]]];
    
    return mainLoginNav;
}


#pragma mark - Public method

- (void)setRootByHome:(BOOL)isAnimation{
    if (isAnimation) {
        CATransition *transtion = [CATransition animation];
        transtion.duration = 0.5;
        [transtion setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
        [transtion setType:@"oglFlip"];
        [transtion setSubtype:kCATransitionFromLeft];
        [kAppDelegate.window.layer addAnimation:transtion forKey:@"transtionKey"];
    }
    self.tabBar = nil;
    kAppDelegate.window.rootViewController = self.tabBar;
}

- (void)setRootByLogin:(BOOL)isAnimation{
    
    ApeNavigationController *mainLoginNav = [self loginNC];
    
    if (isAnimation) {
        CATransition *transtion = [CATransition animation];
        transtion.duration = 0.5;
        [transtion setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
        [transtion setType:@"oglFlip"];
        [transtion setSubtype:kCATransitionFromLeft];
        [kAppDelegate.window.layer addAnimation:transtion forKey:@"transtionKey"];
    }
    
    kAppDelegate.window.rootViewController = mainLoginNav;
}

- (void)gotoLogin:(BOOL)showAlert{
    if (showAlert) {
        kWeakSelf;
//        [UIAlertView alertViewWithTitle:@"提   醒" message:@"该功能需要登录，您确定要登录？" cancelButtonTitle:@"取消" otherButtonTitles:@[@"确定"] onDismiss:^(int buttonIndex) {
            [weakSelf presentLogin];
//        } onCancel:^{
//            
//        }];
    }else{
        [self presentLogin];
    }
}

- (void)presentLogin{
    ApeNavigationController *mainLoginNav = [self loginNC];
    mainLoginNav.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self.homeNC presentViewController:mainLoginNav animated:YES completion:nil];
}

- (void)backHome {
    [[self.tabBar viewControllerInTabWithIndex:0].navigationController popToRootViewControllerAnimated:YES];
    [self.tabBar setSelectedIndex:0];
    //    [self.homeNC.navigationController popToRootViewControllerAnimated:YES];
}

#pragma mark - private method


@end
